package kr.hkit.powerjava.chapter24.work;

import java.util.*;

import kr.hkit.powerjava.chapter24.work.*;
import kr.hkit.powerjava.chapter24.work2.dao.*;

public abstract class ServiceItem<T> {
	protected InterfaceDao<T> dao;
	protected Scanner sc;
	
	public ServiceItem(InterfaceDao<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
	}
	
	protected void init() {
		dao.loadFile();
		prnMenu();
	}

	protected void prnMenu() {
		T item = null;
		int res = 0;
		do{
			System.out.print("1.입력\n2.삭제\n3.수정\n4.검색\n5.리스트보기\n6.종료");
			res = sc.nextInt();
		switch(res){
			case 1:
				//학생정보입력
				item = inputItemInfo(true); //학번, 성명, 국,영,수
				dao.insertItem(item);
				break;
			case 2:
				//삭제, 리스트를 보여주고 번호를 입력받음
				dao.prnItemList(null);
				System.out.print("삭제할 학생정보를 입력하시오");
				item = inputItemInfo(false);
				dao.deleteItem(item);
				break;
			case 3:
				//수정, 리스트를 보여주고 수정할 학생정보를 입력받음
				dao.prnItemList(null);				
				item = inputItemInfo(true);
				dao.updateItem(item);
				break;
			case 4:
				//검색
				item = inputItemInfo(false); //학번, 성명
				try{
				item = dao.searchItem(item);
				}catch(ArrayIndexOutOfBoundsException e){
					System.out.println("학생 정보 없음");
				}
				break;
			case 5:
				prnSubMenu();
				break;
			case 6:
				dao.saveFile();
				sc.close();
				System.exit(0);
				break;
			default:
				res = 6;
		}
		}while(res<6);
	}

	public abstract T inputItemInfo(boolean gubun);
	public abstract void prnSubMenu();
}
